package com.demo.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class DemoIntercepter implements HandlerInterceptor {
    /**
     * 前置拦截器：在真正需要执行的方法之前拦截，返回true表示放行，返回false表示不放行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("前置拦截器");
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /**
     * 后置拦截器：在方法执行之后，返回ModelAndView之前执行
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("后置拦截器");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * 最终拦截器：在整个请求结束之后（即视图渲染完毕之后）执行。数据还没有给客户端响应回去，即将触发给客户端响应数据
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("最终拦截器");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
